package com.ct.goods.responsity.front;

import com.ct.goods.mapper.GoodsSeckillStockMapper;
import com.ct.goods.orm.GoodsSeckillStock;
import com.ct.repository.commons.BaseServiceImpl;
import org.springframework.stereotype.Repository;

/**
 * 秒杀商品分段库存 仓库
 *
 * @author system
 */
@Repository
public class FrontGoodsSeckillStockRepository extends BaseServiceImpl<GoodsSeckillStockMapper, GoodsSeckillStock> {



    public GoodsSeckillStock getByGoodsId(Long goodsId,int segmentIndex) {
        if (goodsId == null) {
            return null;
        }
        return this.lambdaQuery()
                .eq(GoodsSeckillStock::getGoodsId, goodsId)
                .eq(GoodsSeckillStock::getSegmentIndex, segmentIndex)
                .last(" limit 1 ")
                .one();
    }

    /**
     * 修改秒杀商品库存和已售数量
     */
    public boolean decreStock(Long goodsId,int stock, int segmentIndex) {
        return this.lambdaUpdate()
                .set(GoodsSeckillStock::getSegmentStock,stock)
                .eq(GoodsSeckillStock::getGoodsId,goodsId)
                .eq(GoodsSeckillStock::getSegmentIndex,segmentIndex)
                .update();
    }
}
